package com.javastar.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.*;
import java.util.regex.Pattern;

/**
 * hibernate-validator校验工具类
 * 参考文档：http://docs.jboss.org/hibernate/validator/6.0/reference/en-US/html_single/
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class ValidatorUtils {

    // 是否为整数
    private static final String IS_INTEGER_PATTERN = "^[-\\+]?[\\d]*$";
    //
    private static final String MOBILE_NUMBER_PATTERN =
            "\\+(9[976]\\d|8[987530]\\d|6[987]\\d|5[90]\\d|42\\d|3[875]\\d|\n"
                    + "2[98654321]\\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|\n"
                    + "4[987654310]|3[9643210]|2[70]|7|1)\\d{1,14}$";

    private static ResourceBundleMessageSource getMessageSource() {
        ResourceBundleMessageSource bundleMessageSource = new ResourceBundleMessageSource();
        bundleMessageSource.setDefaultEncoding(StandardCharsets.UTF_8.displayName());
        bundleMessageSource.setBasenames("i18n/validation");
        return bundleMessageSource;
    }

    /**
     * 验证值是否是空
     *
     * @param object 验证值
     * @return 验证值是否是空
     */
    public static boolean isEmpty(Object object) {
        boolean isEmpty = false;

        if (object == null) {
            isEmpty = true;
        }
        // 验证字符串类型
        else if (object instanceof String) {
            String validatedObject = (String) object;
            if (validatedObject == null || "".equals(validatedObject.trim())) {
                isEmpty = true;
            }
        }
        // 验证集合类型
        else if (object instanceof Collection) {
            Collection validatedObject = (Collection) object;
            if (validatedObject == null || validatedObject.size() == 0) {
                isEmpty = true;
            }
        }
        // 验证Map类型
        else if (object instanceof Map) {
            Map validatedObject = (Map) object;
            if (validatedObject == null || validatedObject.size() == 0) {
                isEmpty = true;
            }
        }
        // 验证日期类型
        else if (object instanceof Date) {
            Date validatedObject = (Date) object;
            if (validatedObject == null) {
                isEmpty = true;
            }
        }
        // 验证日期类型
        else if (object instanceof Timestamp) {
            Timestamp validatedObject = (Timestamp) object;
            if (validatedObject == null) {
                isEmpty = true;
            }
        }
        // 验证Set类型
        else if (object instanceof Set) {
            Set validatedObject = (Set) object;
            if (validatedObject == null || validatedObject.size() == 0) {
                isEmpty = true;
            }
        } else if (object instanceof String) {
            if (((String) object).trim().equals("")) {
                isEmpty = true;
            }
        }

        return isEmpty;
    }

    /***
     * 验证中国手机号
     *
     * @param mobile
     * @return
     */
    public static boolean isValidMobileNumber(String mobile) {
        return isEmpty(mobile) ? false : mobile.matches(MOBILE_NUMBER_PATTERN);
    }
    /*
     * 判断是否为整数
     * @param str 传入的字符串
     * @return 是整数返回true,否则返回false
     */

    public static boolean isInteger(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile(IS_INTEGER_PATTERN);
        return pattern.matcher(str).matches();
    }

    /***
     * 判断编号是否为空
     *
     * @param id
     * @return
     */
    public static boolean isIdEmpty(Long id) {
        return ValidatorUtils.isEmpty(id) || Objects.equals(id, 0L);
    }
}